package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.po.coladmin.AssociateClassAdminPo;
import com.xjc.business.entity.po.coladmin.ClassPo;
import com.xjc.business.entity.vo.coladmin.ClassVo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.coladmin.MajorVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.ClazzsMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【clazzs(班级表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:31
*/
@Service
public class ClazzsServiceImpl extends ServiceImpl<ClazzsMapper, Clazzs>
    implements ClazzsService {

    @Autowired
    @Lazy
    CollageMajorRelationsService collageMajorRelationsService;

    @Autowired
    @Lazy
    CollageClazzAdminRelationsService collageClazzAdminRelationsService;

    @Autowired
    @Lazy
    MajorClazzRelationsService majorClazzRelationsService;

    @Autowired
    @Lazy
    ClassAdminRelationsService classAdminRelationsService;

    @Autowired
    @Lazy
    MajorsService majorsService;

    @Autowired
    @Lazy
    ClassAdminsService classAdminsService;

    /**
     * 获取该学院下的所有班级
     * @param collegeId 学院id
     * @return 数据
     */
    @Override
    public List<ClassVo> getClassList(Integer collegeId){
        // 该学院下的所有专业id
        LambdaQueryWrapper<CollageMajorRelations> collageMajorRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageMajorRelationsLambdaQueryWrapper.eq(CollageMajorRelations::getColId,collegeId);
        List<Integer> allMajorIdList = collageMajorRelationsService.list(collageMajorRelationsLambdaQueryWrapper).stream().map(CollageMajorRelations::getMajId).collect(Collectors.toList());

        if(allMajorIdList.isEmpty()){
            return new ArrayList<>();
        }

        LambdaQueryWrapper<Clazzs> clazzsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        clazzsLambdaQueryWrapper.in(Clazzs::getId,allMajorIdList);
        List<Clazzs> clazzsList = list(clazzsLambdaQueryWrapper);
        List<ClassVo> classVoList = clazzsList.stream().map(clazzs -> {
            ClassVo classVo = new ClassVo();
            classVo.setId(clazzs.getId());
            classVo.setNum(clazzs.getNum());
            return classVo;
        }).collect(Collectors.toList());
        return classVoList;
    }

    /**
     * 分页查询班级列表
     *
     * @param pageSize     每页显示条数
     * @param currentPage  当前页码
     * @param collegeId    学院id
     * @param majorId      专业id
     * @param classAdminId 辅导员id
     * @return 数据
     */
    @Override
    public Result<PageVo<ClassVo>> getClassPage(Integer pageSize, Integer currentPage, Integer collegeId, Integer majorId, Integer classAdminId) {

        PageVo<ClassVo> classVoPageVo = new PageVo<>();

        // 该学院下的所有专业id
        LambdaQueryWrapper<CollageMajorRelations> collageMajorRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageMajorRelationsLambdaQueryWrapper.eq(CollageMajorRelations::getColId,collegeId);
        List<Integer> allMajorIdList = collageMajorRelationsService.list(collageMajorRelationsLambdaQueryWrapper).stream().map(CollageMajorRelations::getMajId).collect(Collectors.toList());

        // 没有专业-没有班级
        if (allMajorIdList.isEmpty()){
            return Result.okWithData(classVoPageVo);
        }

        // 该学院下的所有专业下所有班级id
        LambdaQueryWrapper<MajorClazzRelations> majorClazzRelationsLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        majorClazzRelationsLambdaQueryWrapper1.in(MajorClazzRelations::getMajId,allMajorIdList);
        List<Integer> allClassIdList = majorClazzRelationsService.list(majorClazzRelationsLambdaQueryWrapper1).stream().map(MajorClazzRelations::getClId).collect(Collectors.toList());

        // 没有班级
        if(allClassIdList.isEmpty()){
            return Result.okWithData(classVoPageVo);
        }

        // 最终需要的班级id-保留重复部分-即经过筛选后的结果
        List<Integer> endIdList = new ArrayList<>(allClassIdList);

        // 指定专业范围
        if(majorId!=-1){
            LambdaQueryWrapper<MajorClazzRelations> majorClazzRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            majorClazzRelationsLambdaQueryWrapper2.eq(MajorClazzRelations::getMajId,majorId);
            List<Integer> classIdListWithMajorId  = majorClazzRelationsService.list(majorClazzRelationsLambdaQueryWrapper2).stream().map(MajorClazzRelations::getClId).collect(Collectors.toList());

            endIdList.retainAll(classIdListWithMajorId);

        }

        // 指定辅导员范围
        if(classAdminId!=-1){
            LambdaQueryWrapper<ClassAdminRelations> classAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            classAdminRelationsLambdaQueryWrapper.eq(ClassAdminRelations::getClazzAdminId,classAdminId);
            List<Integer> classIdListWithClassAdminId = classAdminRelationsService.list(classAdminRelationsLambdaQueryWrapper).stream().map(ClassAdminRelations::getClId).collect(Collectors.toList());

            endIdList.retainAll(classIdListWithClassAdminId);

        }

        // 没有满足条件的班级
        if(endIdList.isEmpty()){
            return Result.okWithData(classVoPageVo);
        }

        // 分页
        LambdaQueryWrapper<Clazzs> clazzsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        clazzsLambdaQueryWrapper.in(Clazzs::getId,endIdList);

        Page<Clazzs> clazzsPage = new Page<>(currentPage, pageSize);
        page(clazzsPage,clazzsLambdaQueryWrapper);

        List<Clazzs> clazzsPageRecords = clazzsPage.getRecords();
        List<ClassVo> classVoList = clazzsPageRecords.stream().map(clazzs -> {
            ClassVo classVo = new ClassVo();
            classVo.setId(clazzs.getId());
            classVo.setNum(clazzs.getNum());
            classVo.setCreateTime(clazzs.getCreateTime());

            // 所属专业名称
            LambdaQueryWrapper<MajorClazzRelations> majorClazzRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            majorClazzRelationsLambdaQueryWrapper.eq(MajorClazzRelations::getClId,clazzs.getId());
            MajorClazzRelations majorClazzRelations = majorClazzRelationsService.getOne(majorClazzRelationsLambdaQueryWrapper);
            classVo.setMajorName(majorsService.getById(majorClazzRelations.getMajId()).getName());

            // 获取辅导员名称
            LambdaQueryWrapper<ClassAdminRelations> classAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            classAdminRelationsLambdaQueryWrapper.eq(ClassAdminRelations::getClId,clazzs.getId());
            ClassAdminRelations classAdminRelations = classAdminRelationsService.getOne(classAdminRelationsLambdaQueryWrapper);
            if(classAdminRelations == null){
                classVo.setAdminName("暂未分配");
            }else {

                classVo.setAdminName(classAdminsService.getById(classAdminRelations.getClazzAdminId()).getNickname());
            }

            return classVo;
        }).collect(Collectors.toList());
        classVoPageVo.setData(classVoList);
        classVoPageVo.setTotal(clazzsPage.getTotal());
        classVoPageVo.setCurrent(currentPage);
        classVoPageVo.setSize(pageSize);
        classVoPageVo.setPageCount(clazzsPage.getPages());
        return Result.okWithData(classVoPageVo);
    }

    /**
     * 添加班级
     * @param classPo 班级信息
     * @return 反馈
     */
    @Override
    @Transactional
    public Result<String> addClass(ClassPo classPo) {
        Integer majorId = classPo.getMajorId();
        Integer num = classPo.getNum();

        LambdaQueryWrapper<MajorClazzRelations> majorClazzRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        majorClazzRelationsLambdaQueryWrapper.eq(MajorClazzRelations::getMajId,majorId);
        List<Integer> collectIdList = majorClazzRelationsService.list(majorClazzRelationsLambdaQueryWrapper).stream().map(MajorClazzRelations::getClId).collect(Collectors.toList());

        if(!collectIdList.isEmpty()){
            LambdaQueryWrapper<Clazzs> clazzsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            clazzsLambdaQueryWrapper.eq(Clazzs::getNum,num);
            clazzsLambdaQueryWrapper.in(Clazzs::getId,collectIdList);
            if (count(clazzsLambdaQueryWrapper) > 0) {
                return Result.ok("班级已经存在");
            }
        }

        Clazzs clazzs = new Clazzs();
        clazzs.setNum(num);
        clazzs.setCreateTime(new Date());
        clazzs.setUpdateTime(new Date());

        save(clazzs);

        MajorClazzRelations majorClazzRelations = new MajorClazzRelations();
        majorClazzRelations.setMajId(majorId);
        majorClazzRelations.setClId(clazzs.getId());

        majorClazzRelationsService.save(majorClazzRelations);

        return Result.okWithData("添加成功");
    }

    /**
     * 分配管理员
     * @param associateClassAdminPo 管理员信息
     * @return 反馈
     */
    @Override
    public Result<String> associateClassAdmin(AssociateClassAdminPo associateClassAdminPo) {
        Integer adminId = associateClassAdminPo.getAdminId();
        Integer classId = associateClassAdminPo.getClassId();

        ClassAdminRelations classAdminRelations = new ClassAdminRelations();
        classAdminRelations.setClazzAdminId(adminId);
        classAdminRelations.setClId(classId);
        classAdminRelationsService.save(classAdminRelations);

        return Result.okWithData("分配成功");
    }

    /**
     * 分页查询班级信息
     *
     * @param classAdminId 辅导员id
     * @param majorId      专业id
     * @return 数据
     */
    @Override
    public Result<List<com.xjc.business.entity.vo.classadmin.ClassVo>> getClassPage2(Integer classAdminId, Integer majorId) {

        List<com.xjc.business.entity.vo.classadmin.ClassVo> classVoPageVo =  new ArrayList<>();

        LambdaQueryWrapper<ClassAdminRelations> classAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classAdminRelationsLambdaQueryWrapper.eq(ClassAdminRelations::getClazzAdminId,classAdminId);
        List<Integer> classIdList = classAdminRelationsService.list(classAdminRelationsLambdaQueryWrapper).stream().map(ClassAdminRelations::getClId).collect(Collectors.toList());

        if(classIdList.isEmpty()){
            return Result.okWithData(classVoPageVo);
        }

        if(majorId!=-1){
            LambdaQueryWrapper<MajorClazzRelations> majorClazzRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            majorClazzRelationsLambdaQueryWrapper.eq(MajorClazzRelations::getMajId,majorId);
            List<Integer> collectIdList = majorClazzRelationsService.list(majorClazzRelationsLambdaQueryWrapper).stream().map(MajorClazzRelations::getClId).collect(Collectors.toList());
            classIdList.retainAll(collectIdList);
        }

        if(classIdList.isEmpty()){
            return Result.okWithData(classVoPageVo);
        }

        LambdaQueryWrapper<Clazzs> clazzsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        clazzsLambdaQueryWrapper.in(Clazzs::getId,classIdList);
        List<Clazzs> clazzsList = list(clazzsLambdaQueryWrapper);
        List<com.xjc.business.entity.vo.classadmin.ClassVo> classVoList = clazzsList.stream().map(clazzs -> {
            com.xjc.business.entity.vo.classadmin.ClassVo classVo = new com.xjc.business.entity.vo.classadmin.ClassVo();
            classVo.setId(clazzs.getId());
            classVo.setNum(clazzs.getNum());
            classVo.setCreateTime(clazzs.getCreateTime());
            Majors majors = majorsService.getById(majorClazzRelationsService.getById(majorClazzRelationsService.getOne(new LambdaQueryWrapper<MajorClazzRelations>().eq(MajorClazzRelations::getClId, clazzs.getId()))).getMajId());
            classVo.setMajorName(majors.getName());
            classVo.setMajorId(majors.getId());
            return classVo;
        }).collect(Collectors.toList());

        return Result.okWithData(classVoList);
    }

    /**
     * 获取专业列表
     *
     * @param classAdminId 辅导员id
     * @return 数据
     */
    @Override
    public Result<List<MajorVo>> getMajorList(Integer classAdminId) {
        List<com.xjc.business.entity.vo.classadmin.ClassVo> classVoList = getClassPage2(classAdminId, -1).getData();
        List<MajorVo> majorVoList = new ArrayList<>();
        Map<Integer,String> map = new HashMap<>();
        for (com.xjc.business.entity.vo.classadmin.ClassVo classVo : classVoList) {
            map.put(classVo.majorId,classVo.majorName);
        }

        for (Integer key : map.keySet()) {
            MajorVo majorVo = new MajorVo();
            majorVo.setId(key);
            majorVo.setName(map.get(key));
            majorVoList.add(majorVo);
        }

        return Result.okWithData(majorVoList);
    }

}




